package com.molyfun.parents.activity.store.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.molyfun.parents.R;
import com.molyfun.parents.activity.store.ChooseChildrenInfoActivity;
import com.molyfun.parents.rest.bean.OrganizationListResponse;
import com.molyfun.parents.utils.LogUtils;
import com.molyfun.parents.utils.PreferencesStorageUtil;
import com.molyfun.parents.view.TwoTabBar;

import java.util.HashMap;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * Created by william(liuchao.hit@gmail.com).
 * Date:9/19/16.
 */
public class ChooseChildrenListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    /**
     * TYPE_ADDED 已加入
     * TYPE_CHECK 审核中
     */
    private int mCurrentTab = TwoTabBar.TwoTabChooseTab.BAR_ONE;
    private Context mContext;
    private int mOldChoosePosition = -1;
    private int mSelectedPosition = -1;
//    private HashMap<String, String> departIdList = new HashMap<String, String>();

    private List<OrganizationListResponse.Organization> mOrganizationList;
    private String departName = "";

    public ChooseChildrenListAdapter(Context mContext) {
        this.mContext = mContext;
    }

    public void refreshData(List<OrganizationListResponse.Organization> list,String departName) {
        this.mOrganizationList = list;
        this.departName = departName;
        int position = 0;
        for (OrganizationListResponse.Organization organization : list) {
            if (organization.isSelected()) {
                mOldChoosePosition = position;
                mSelectedPosition = mOldChoosePosition;
            }
            position++;

        }
        notifyDataSetChanged();
    }

    public void clearAll() {
        if (mOrganizationList != null) {
            mOrganizationList.clear();
            notifyDataSetChanged();
        }
    }

    public OrganizationListResponse.Organization getChoosedOrganization() {
        if (mOldChoosePosition == mSelectedPosition) {

//                return null;

        }
        if(mSelectedPosition == -1) {
            return null;
        }else {
            return mOrganizationList.get(mSelectedPosition);
        }

    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new OrganizationViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_organization_children_listl, parent, false));
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        OrganizationViewHolder organizationViewHolder = (OrganizationViewHolder) holder;
        final OrganizationListResponse.Organization organizationBean = mOrganizationList.get(position);

//        if (PreferencesStorageUtil.getDepartname(mContext).equals(organizationBean.getDepartname())) {
        if (departName.equals(organizationBean.getDepartname())) {
            organizationViewHolder.mStudentRemarkName.setText(organizationBean.getStudentremakname());
            organizationViewHolder.mStudentRemarkName.setTextColor(mContext.getResources().getColor(R.color.color_text_setting));//
            organizationViewHolder.mStudentRemarkName.setTextSize(13);

            organizationViewHolder.mOrganizationCheckState.setVisibility(View.VISIBLE);
            if (mSelectedPosition == position) {
                organizationViewHolder.mOrganizationCheckState.setImageResource(R.mipmap.ic_pay_checkbox_choosed);

            } else {
                organizationViewHolder.mOrganizationCheckState.setImageResource(R.mipmap.ic_pay_checkbox_normal);
            }
            organizationViewHolder.mOrganizationCheckState.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mSelectedPosition = position;
//                    departIdList.put("departId"+position,organizationBean.getId());
                    notifyDataSetChanged();
                }
            });
        } else {
            organizationViewHolder.mOrganizationCheckState.setVisibility(View.GONE);
            organizationViewHolder.mStudentRemarkName.setVisibility(View.GONE);
        }

    }

    @Override
    public int getItemCount() {

        if (mOrganizationList == null) {
            return 0;
        } else {
            return mOrganizationList.size();
        }
    }

    class OrganizationViewHolder extends RecyclerView.ViewHolder {

        @InjectView(R.id.organization_check_state)
        ImageView mOrganizationCheckState;
        @InjectView(R.id.student_remarkname)
        TextView mStudentRemarkName;

        public OrganizationViewHolder(View view) {
            super(view);
            ButterKnife.inject(this, view);
        }
    }

}
